﻿// Copyright (c) Vitaly Kamiansky. All rights reserved. 
// See License.txt in the project root.

using System;

namespace Property.Workers
{
    public abstract class WorkerDecorator<T> : Worker<T>
    {
        protected WorkerDecorator(IWorkerPrototype<T> compositionBasePrototype, Func<T> getValue)
        {
            if (compositionBasePrototype == null) throw new ArgumentNullException("compositionBasePrototype");

            _CompositionBase = compositionBasePrototype.Create(getValue);

            if (_CompositionBase == null)
                throw new NotSupportedException(StringsCallbacks.CANT_OBTAIN_WORKER);

            _CompositionBase.Finished += Finished;
        }

        //=//

        protected readonly IWorker<T> _CompositionBase;

        public abstract override void Go();

        public override bool IsRunning { get { return _CompositionBase.IsRunning; } }

        public override T Result { get { return _CompositionBase.Result; } }

        public override Exception Exception { get { return _CompositionBase.Exception; } }

        protected abstract void Finished(object sender, EventArgs e);

        public override void Dispose()
        {
            _CompositionBase.Finished -= Finished;
            _CompositionBase.Dispose();
        }
    }
}
